package cn.wxManage.goodsManage.service.impl;

import cn.common.common.model.BaseStatus;
import cn.common.common.model.ServerResponse;
import cn.wxManage.goodsManage.dao.GoodsCartMapper;
import cn.wxManage.goodsManage.model.GoodsCart;
import cn.wxManage.goodsManage.service.IGoodsCartService;
import cn.wxManage.user.model.UserInfo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by Zhang on 2019/1/22.
 */
@Service
public class GoodsCartService implements IGoodsCartService{
    @Autowired
    HttpServletRequest request;

    @Autowired
    GoodsCartMapper goodsCartMapper;

    @Override
    public ServerResponse<?> addGoodsToCart(GoodsCart goodsCart) {
        UserInfo userInfo = getUserInfo();
        GoodsCart goodsCartOld = goodsCartMapper.isExist(userInfo.getUserid(),goodsCart.getGoodsId(),goodsCart.getGoodsStyleId());
        if (goodsCartOld!=null){
            goodsCartOld.setStyleNum(goodsCartOld.getStyleNum() + goodsCart.getStyleNum());
            goodsCartMapper.updateByPrimaryKeySelective(goodsCartOld);
        }else {
            goodsCart.setUserId(userInfo.getUserid());
            goodsCart.setUserName(userInfo.getUsername());
            goodsCart.setStatus(BaseStatus.cartPendingEntry);
            goodsCart.setCreateTime(new Date());
            goodsCartMapper.insertSelective(goodsCart);
        }
        return ServerResponse.createBySuccessMessage("操作成功");
    }

    @Override
    public ServerResponse<?> updateCartNum(GoodsCart goodsCart) {
        if (goodsCart.getStyleNum()<=0){
            goodsCartMapper.deleteById(goodsCart.getId());
        }
        GoodsCart tem = new GoodsCart();
        tem.setId(goodsCart.getId());
        tem.setStyleNum(goodsCart.getStyleNum());
        goodsCartMapper.updateByPrimaryKeySelective(tem);
        return ServerResponse.createBySuccessMessage("操作成功");
    }

    @Override
    public ServerResponse<?> deleteGoodsFromCart(Integer id,String type) {
        if ("goods".equals(type)){
            UserInfo userInfo = getUserInfo();
            goodsCartMapper.deleteByGoodsId(id,userInfo.getUserid());
        }else if ("goodsStyle".equals(type)){
            goodsCartMapper.deleteById(id);
        }else {
            return ServerResponse.createByErrorMessage("参数错误");
        }
        return ServerResponse.createBySuccessMessage("操作成功");
    }

    @Override
    public ServerResponse<?> getCartList() {
        UserInfo userInfo = getUserInfo();
        List<GoodsCart> goodsCartList = goodsCartMapper.getCartList(userInfo.getUserid());
        return ServerResponse.createBySuccess(goodsCartList);
    }

    @Override
    public ServerResponse<?> getCartNum() {
        UserInfo userInfo = getUserInfo();
        Integer num = 0;
        List<GoodsCart> goodsCartList = goodsCartMapper.getCartList(userInfo.getUserid());
        for (GoodsCart goodsCart : goodsCartList){
            num += goodsCart.getStyleNum();
        }
        return ServerResponse.createBySuccess(num);
    }

    private UserInfo getUserInfo(){
        return (UserInfo) request.getAttribute("userInfo");
    }
}
